<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//    out.println(basePath);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">


    <title>login</title>

    <link href="<%=basePath %>static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>

<h1 style="text-align:center">讯百测试项目</h1>
<hr><br>

<div class="row" style="padding: 100px 100px 0 0">
    <div class="col-md-5"></div>
    <div class="col-md-3">

        <form>
            <div class="form-group">
                <label for="exampleInputEmail1">登陆</label>
                <input type="text" class="form-control" id="tel" name="tel" placeholder="账号">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" id="password" name="password" placeholder="密码">
            </div>
            <input type="button" id="submit" class="btn btn-primary btn-lg btn-block" value="登陆"/>

        </form>
    </div>
    <div class="col-md-3"></div>
</div>
<script src="<%=basePath %>static/jquery/jquery-3.3.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="<%=basePath %>static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script>


    function func() {
        //验证用户是否已经注册过
        var tel = $("#tel").val();//获取表单元素值
        var password = $("#password").val();

        var tel = document.getElementById('tel').value;
        if(tel==""){
            alert("账号不能为空！");
            return false;
        }
        if(password=="")
        {
            alert("密码不能为空！");
            return false;
        }
        if(password.length<4){
            alert("密码不能少于4位！");
            return false;
        }
        return true;
    }

    $("#submit").on("click", function() {
        if (!func()) {
            return false;
        }
        var tel = $("#tel").val();//获取表单元素值
        var password = $("#password").val();
        var data = {
            tel : tel,
            password : password
        };
        $.ajax({
            type : "POST",
            url : "/login",
            data : data,
            dataType : 'json',
            success : function(msg) {
                if ("00"==msg.errorCode) {
                    console.log("success");
                    window.location.href = "/test/toIndex";
                } else if("11"==msg.errorCode) {
                    alert("账号不存在！");
                    return false;
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // 状态码
                console.log(XMLHttpRequest.status);
                // 状态
                console.log(XMLHttpRequest.readyState);
                // 错误信息
                console.log(textStatus);
            }

        });
    });
</script>

</body>
</html>